package com.example.big_event.service.impl;

import com.example.big_event.mapper.CategoryMapper;
import com.example.big_event.pojo.Category;
import com.example.big_event.pojo.Result;
import com.example.big_event.service.CategoryService;
import com.example.big_event.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service//声明这是service层
public class CategoryServiceimpl implements CategoryService {

    @Autowired//注解注入mapoer对象
    CategoryMapper mapper;


    @Override //添加分类 传入分类对象
    public void add(Category category) {

        //设置分类对象的创建和修改时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        //ThreadLocalUtil中获取到分类id
        Map<String,Object> map = ThreadLocalUtil.get();

        Integer id = (Integer) map.get("id");

        //id存入category对象中
        category.setId(id);

        //mapper传入category对象调用dao层方法
        mapper.add(category);

    }

    @Override //查询所有分类   无需传参
    public List<Category> select() {


        //TreadLocalUtil获取当前用户id
        Map<String,Object> map = ThreadLocalUtil.get();

        Integer id = (Integer) map.get("id");

        //dao层根据用户id查询到分类数据 返回一个List的category类型集合
        List<Category> list = mapper.select(id);

        //返回集合数据
        return list;
    }

    @Override//根据id查找分类数据
    public List<Category> findByid(Integer id) {

        //mapper根据id查找到category返回category类型的集合
        List<Category> list = mapper.findByid(id);

        //返回集合
        return list;


    }

    @Override //删除分类  传入分类id
    public void delete(Integer id) {

        //delete category depend on id
        mapper.delete(id);

    }

    @Override //update catgory
    public void update(Category category) {

        //update update-time in category class before you use mapper
        category.setUpdateTime(LocalDateTime.now());
        //mapper调用方法 传入category对象
        mapper.update(category);

    }
}
